package com.jinxin.phonecloudmusic.myapplication.fragment.fra;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.jinxin.phonecloudmusic.myapplication.R;
import com.jinxin.phonecloudmusic.myapplication.adapter.MusicListAdapter;
import com.jinxin.phonecloudmusic.myapplication.base.BaseFragment;
import com.jinxin.phonecloudmusic.myapplication.entity.GroupBean.BillboardList;
import com.jinxin.phonecloudmusic.myapplication.entity.MusicBean.SongList;
import com.jinxin.phonecloudmusic.myapplication.event.MusicControlEvent;
import com.jinxin.phonecloudmusic.myapplication.presenter.IPresenter.IMainPagePresenter;
import com.jinxin.phonecloudmusic.myapplication.presenter.PresenterImpl.IMainPagePresentImp;
import com.jinxin.phonecloudmusic.myapplication.utils.FragmentUtil;
import com.jinxin.phonecloudmusic.myapplication.utils.RxBus;
import com.jinxin.phonecloudmusic.myapplication.view.IMusicListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ZJ on 2017/10/17 0017.
 * 音乐列表
 */

public class MusicListFragment extends BaseFragment implements IMusicListView {
    @BindView(R.id.lv_music_list_detail)
    ListView lvMusicListDetail;
    @BindView(R.id.iv_policy_search)
    ImageView ivPolicySearch;
    @BindView(R.id.iv_music_list_back)
    ImageView back;
    //数据中转站
    private List<SongList> songLists;
    private int type;
    private int pos;

    private MusicListAdapter musicListAdapter;

    private IMainPagePresenter iMainPagePresenter;
    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setStyle(android.support.v4.app.DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat);
        //接收数据
        if (null!=getArguments()) {
            type = (int) getArguments().get("type");
            //判空处理
        }

        iMainPagePresenter=new IMainPagePresentImp(this);
        iMainPagePresenter.BillboardList("billboard", type, 20, 0);

        if (null!=ivPolicySearch){
            ivPolicySearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
    }

    @Override
    public View inflate(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fra_music_list, container, false);
        unbinder = ButterKnife.bind(this,view);
        //initView(view);
        view.setFocusable(true);//这个和下面的这个命令必须要设置了，才能监听back事件。
        view.setFocusableInTouchMode(true);
        view.setOnKeyListener(backlistener);
        init();//初始化
        return view;
    }

    @Override
    public void initData() {

    }

    public void init() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回到主界面
                RxBus.getDefault().post(new MusicControlEvent(32, "", ""));
                //FragmentUtil.popTopInclude(getFragmentManager(),"musicListFragment");
            }
        });
    }

    //干掉回退键
    private View.OnKeyListener backlistener = new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                if (i == KeyEvent.KEYCODE_BACK) {  //表示按返回键 时的操作
                    FragmentUtil.popTopInclude(getFragmentManager(),"musicListFragment");
                    Log.e("---musicList","点击了");
                    return true;    //已处理
                }
            }
            return true;
        }
    };

    @Override
    public void setListView(BillboardList billboardList) {
        songLists=billboardList.getLists();
        if (null==musicListAdapter){
            musicListAdapter=new MusicListAdapter(getActivity(),R.layout.item_home_page_music_list,songLists);
        }else {
            musicListAdapter.replaceAll(songLists);
        }
        lvMusicListDetail.setAdapter(musicListAdapter);
        lvMusicListDetail.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RxBus.getDefault().post(new MusicControlEvent(30,songLists,position,"musicList"));
            }
        });
    }

    @Override
    public void jumb() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null!=unbinder)
        unbinder.unbind();
    }
}
